package com.lottery.im.widget.dialog;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.ShowTipObserver;
import com.lottery.im.api.result.entity.integral.ExchangeResult;
import com.lottery.im.module.service.ui.CommodityDetailFragment;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;

/**
 * 作者：cyy
 * 时间：2018/9/19。
 */
public class ExchangeDialogFragment extends BaseDialogFragment {

  @BindView(R.id.tv_acount) TextView tvAccount;
  @BindView(R.id.tv_commodityName) TextView tvCommodityName;
  @BindView(R.id.tv_exchangeNum) TextView tvExchangeNum;
  @BindView(R.id.tv_exchangeMoney) TextView tvExchangeMoney;
  Unbinder unbinder;
  private String mUserId;
  private String mToken;
  private int mCount;
  private int mCommodityId;
  private String mUserName;

  public interface ConfirmListener {
    void onConfirmListener(ExchangeResult result);
  }

  private ConfirmListener mConfirmListener;

  public void setOnConfirmListener(ConfirmListener listener) {
    mConfirmListener = listener;
  }

  public static ExchangeDialogFragment newInstance(
      CommodityDetailFragment.ExchangeMessage exchangeMessage) {
    Bundle args = new Bundle();
    args.putSerializable("exchange", exchangeMessage);
    ExchangeDialogFragment fragment = new ExchangeDialogFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override public int getResourceLayout() {
    return R.layout.fragment_exchange_dialog;
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = super.onCreateView(inflater, container, savedInstanceState);
    unbinder = ButterKnife.bind(this, rootView);
    mUserId = MyApplication.getInstance().getUserInfo().getUserId();
    mUserName = MyApplication.getInstance().getUserInfo().getUserName();
    mToken = MyApplication.getInstance().getToken();

    CommodityDetailFragment.ExchangeMessage exchange =
        (CommodityDetailFragment.ExchangeMessage) getArguments().getSerializable("exchange");
    tvAccount.setText(mUserName);
    tvCommodityName.setText(exchange.getCommodityName());
    tvExchangeNum.setText((mCount = exchange.getCount()) + "");
    tvExchangeMoney.setText(exchange.getMonty() + "");

    mCommodityId = exchange.getId();

    return rootView;
  }

  @Override public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
  }

  @OnClick({ R.id.tv_cancel, R.id.tv_confirm }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.tv_cancel:
        dismiss();
        break;
      case R.id.tv_confirm:
        ApiClient.getApiRetrofitInstance()
            .exchangeBuy(mUserId, mToken, mCount, mCommodityId)
            .throttleFirst(2, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new ShowTipObserver<ExchangeResult>(getActivity()) {
              @Override public void onSuccess(ExchangeResult exchangeResult) {
                mConfirmListener.onConfirmListener(exchangeResult);
              }
            });
        dismiss();
        break;
    }
  }
}
